/*
 * @(#)DirectoryRenderer.java 0.0.1-SNAPSHOT
 *
 * Copyright Group B
 */
package grupb.upf.gui;

import java.awt.Component;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 * <p>This is the directory renderer of the JTree. It lets us to modify
 * how it is going to be shown.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public final class DirectoryRenderer extends DefaultTreeCellRenderer {

    /**
     * The size of the icons.
     */
    private int iconSize;
    /**
     * The icon of an openned folder.
     */
    private ImageIcon open;
    /**
     * The icon of a closed folder.
     */
    private ImageIcon closed;

    /**
     * Constructor of the class.
     * @param size the height and width of the icons to use.
     */
    public DirectoryRenderer(final int size) {
        this.iconSize = size;
        final ImageIcon imgOpen =
                new ImageIcon(getClass().getResource("/graphics/open.png"));
        final ImageIcon imgClosed =
                new ImageIcon(getClass().getResource("/graphics/closed.png"));
        open = new ImageIcon(imgOpen.getImage().getScaledInstance(
                iconSize, iconSize, Image.SCALE_SMOOTH));
        closed = new ImageIcon(imgClosed.getImage().getScaledInstance(
                iconSize, iconSize, Image.SCALE_SMOOTH));
    }

    /**
     * The method to paint the elements of the JTree.
     * @param tree the tree to paint
     * @param value the value of the object to paint
     * @param sel if it is selected or not
     * @param expanded if it is expaned or not
     * @param leaf if it is a leaf or not
     * @param row the number of rows
     * @param hasFocus if it is focused
     * @return the component to paint
     */
    @Override
    public Component getTreeCellRendererComponent(final JTree tree,
            final Object value, final boolean sel, final boolean expanded,
            final boolean leaf, final int row, final boolean hasFocus) {

        super.getTreeCellRendererComponent(
                tree, value, sel, expanded, leaf, row, hasFocus);

        if (expanded) {
            setIcon(open);
        } else {
            setIcon(closed);
        }
        return this;
    }
}
